<!--ID:details.html-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../../libs/mui.min.css" rel="stylesheet"/>
    <link href="../../libs/app.css" rel="stylesheet"/>
    <link href="../../css/main.css" rel="stylesheet"/>
    <link href="../../css/details.css" rel="stylesheet"/>
</head>

<body>
<div class="head">
    <img class="mui-action-back" src="../../images/all-left.png" alt="">
    <p>产品详情</p>
    <div class="coll false">
        <img class="icon-collect-false" src="../../images/collect-false.png" alt="">
        <img class="icon-collect-true" src="../../images/collect-true.png" alt="">
        <p class="colect-button">收藏</p>
    </div>
</div>

<div class="mui-scroll-wrapper">
    <div class="mui-scroll">
        <div class="content">
            <div>
                <img src="" alt="" width="100%">
            </div>
            <p class="pro-name"></p>
            <p>详情</p>
            <p>价格：¥<span class="price"></span>元</p>
            <p>会员价：¥<span class="price2"></span>元</p>
        </div>
        <div class="richText"></div>
        <!--
<div class="liang">
    <p>亮点</p>
    <p class="p1">利息：按剩余本金计算，随每月剩余本金变少减少</p>
    <p>利息：按剩余本金计算，随每月剩余本金变少减少</p>
    <p>利息：按剩余本金计算，随每月剩余本金变少减少</p>
</div>
<div class="bian">
    <p>让一切变得简单点</p>
    <p>变得好一点！</p>
    <img src="../../images/wangqiang.png" alt="" width="100%">
</div>
-->

    </div>
</div>

<div class="fix">
    <div class="fastadd">立即加入购物车</div>
    <div class="fastbuy">立即购买</div>
</div>

<script src="../../libs/mui.min.js"></script>
<script src="../../js/jquery-3.1.1.min.js"></script>
<script src="../../js/url.js"></script>
<script src="../../js/userInfo.js"></script>
<script>
    var ERROR_OK = 'S002';
    var vip;

    $(function () {
//        mui.init({
//            beforeback: function () {
//                var list = plus.webview.getWebviewById('helper.html');
//                mui.fire(list, 'refresh');
//                return true;
//            }
//        });
        vip = (localStorage.getItem('vip') == 'true' ? true : false);
        vip && $('.mui-scroll-wrapper').addClass('vip');
        deta().then(function () {
            fastBuy(vip);
        })
    });

    mui('.mui-scroll-wrapper').scroll({deceleration: 0.0010});

    function deta() {
        var def = $.Deferred()
        var id = localStorage.details;
        $.ajax({
            url: ur + 'rest/productController/info-product?productId=' + id,
            type: 'GET',
            headers: {
                "X-Token": localStorage.token
            },
            dataType: 'json',
            data: '',
        })
            .done(function (res) {
                // console.log(JSON.stringify(res));

                /**
                 * 由详情信息标识收藏状态
                 var collect = res.data ? res.data.collect : false;
                 if (collect) {
						    $('.coll').addClass('true').removeClass('false');
						    console.log('已收藏')
						 } else {
						    $('.coll').addClass('false').removeClass('true');
						    console.log('未收藏')
						 }

                 // 由收藏列表标识收藏状态
                 hasCollect(id).then(function (data) {
						    if (data) {
						        $('.coll').addClass('true').removeClass('false');
						    } else {
						        $('.coll').addClass('false').removeClass('true');
						    }
						})
                 **/
                // 图片加载失败也显示
                $('.content img')[0].onerror = function () {
                    $(this).attr('src', '../../images/error.png');
                    $('.mui-scroll-wrapper').addClass('on');
                }

                if (res.meta.code === 'S002') { // 已购买
                    $('.fix').hide(0);
                    $('.content img')[0].onload = function () {
                        $('.mui-scroll-wrapper').addClass('on');
                    }
                    $('.content img')[0].src = ur + res.data.image;
                    $('.price').text(res.data.price);
                    $('.price2').text(res.data.vipPrice);
                    $('.pro-name').text(res.data.name);
                    $('.richText').html(HTMLDecode(res.data.richText));
                    res.data.collect ? $('.coll').addClass('true').removeClass('false') : $('.coll').addClass('false').removeClass('true');

                } else if (res.meta.code === 'E002') { // 未购买

                    //                    var _details = JSON.parse(localStorage._details);
                    $('.content img')[0].onload = function () {
                        $('.mui-scroll-wrapper').addClass('on');
                    }
                    //                    $('.content img')[0].src = _details.imgSrc;
                    //                    $('.price').text(_details.price);
                    //                    $('.price2').text(_details.vipPrice);
                    //                    $('.pro-name').text(_details.name);
                    $('.content img')[0].src = ur + res.data.image;
                    $('.price').text(res.data.price);
                    $('.price2').text(res.data.vipPrice);
                    $('.pro-name').text(res.data.name);
                    $('.liang, .bian').hide(0);
                    res.data.collect ? $('.coll').addClass('true').removeClass('false') : $('.coll').addClass('false').removeClass('true');
                    mui.toast('您还未购买此商品');
                } else {
                    mui.toast(res.meta.message);
                }
                def.resolve();
            });
        return def.promise();
    }

    opencar()
    /*开启购物车*/
    function opencar() {
        $.ajax({
            url: ur + 'rest/productController/order',
            type: 'GET',
            headers: {
                "X-Token": localStorage.token
            },
            dataType: 'json',
            data: '',
        })
            .done(function (data) {
                if (data.meta.code === 'S002') {
                    localStorage.carid = data.data;
                } else {
                    mui.toast(data.meta.message);
                }
            })
    }

    /*加入购物车*/
    $(document).on('tap', '.fastadd', function addcar() {
        var id = localStorage.details;
        $.ajax({
            url: ur + 'rest/productController/add-order',
            type: 'POST',
            dataType: 'json',
            headers: {
                "X-Token": localStorage.token
            },
            data: {
                "orderId": localStorage.carid,
                "productId": id,
                "size": '1',
            }
        })
            .done(function (data) {
                if (data.meta.code == 'S002') {
                    mui.toast('成功加入购物车');
                    localStorage.carNumber++;
                } else {

                }
            })
    })

    /*收藏产品*/
    $(document).on('tap', '.coll', function addcar(event) {
        if ($('.coll').hasClass('true')) { // 删除收藏
            $.ajax({
                url: ur + 'rest/productController/del-collect',
                type: 'POST',
                dataType: 'json',
                headers: {
                    "X-Token": localStorage.token
                },
                data: {
                    "productId": localStorage.details // 根据产品id删除
                }
            }).done(function (res) {
                if (res.meta.code == ERROR_OK) {
                    $('.coll').addClass('false').removeClass('true');
                    mui.toast('取消收藏成功');
                } else {
                    //                  console.log('取消 '+ JSON.stringify(res))
                }
            })
        } else { // 添加收藏
            $.ajax({
                url: ur + 'rest/productController/collect',
                type: 'POST',
                dataType: 'json',
                headers: {
                    "X-Token": localStorage.token
                },
                data: {
                    "productId": localStorage.details,
                    "size": '1',
                }
            }).done(function (res) {
                if (res.meta.code == ERROR_OK) {
                    $('.coll').addClass('true').removeClass('false');
                    mui.toast('收藏成功');
                } else {
                    //                  console.log('收藏 '+ JSON.stringify(res))
                }
            });
        }
    })

    /* 立即购买（单品） */
    function fastBuy(vip) {
        $(document).on('tap', '.fastbuy', function () {
            // 获取价格
            if (vip) {
                localStorage.sureallprice = $('.price2').text();
            } else {
                localStorage.sureallprice = $('.price').text();
            }
            // 购物车
            localStorage.surehow = 1;
            // 请求订单ID
            $.ajax({
                url: ur + 'rest/productController/pay-product',
                type: 'POST',
                headers: {'X-Token': localStorage.token},
                dataType: 'json',
                data: {'productId': localStorage.details}
            }).done(function (res) {
                if (res.meta.code === ERROR_OK) {
                    // console.log('singleOrderID=' + res.data);
//                    href('sure.html', 'pay', {singleOrderID: res.data});
                    localStorage.setItem('details-sure', JSON.stringify({
                        singleOrderID: res.data
                    }));
                    href('sure.html', 'pay');
                } else {
                    mui.toast(res.meta.message);
                }
            });
        });
    }

    // 获取所有收藏列表数据,对比收藏产品id
    function hasCollect(id) {
        var o = {},
            def = $.Deferred();
        $.ajax({
            url: ur + 'rest/productController/collect-list',
            type: 'GET',
            headers: {
                "X-Token": localStorage.token
            },
            dataType: 'json',
            data: {}
        }).done(function (res) {
            if (res.meta.code === ERROR_OK && res.data.list.length > 0) {
                var list = res.data.list;
                for (var i in list) {
                    o[list[i].product.id] = true;
                }
            }
            def.resolve(o.hasOwnProperty(id));
        });
        return def.promise();
    }

    window.addEventListener('refresh', function () {
        vip && $('.mui-scroll-wrapper').addClass('vip');
        deta().then(function () {
            fastBuy(vip);
        });
    }, false);
</script>

</body>

</html>